package com.fengze.business.service.impl;

import java.io.IOException;
import java.util.List;

import com.fengze.business.domain.FzProductsCategory;
import com.fengze.business.mapper.FzProductsCategoryMapper;
import com.fengze.business.mapper.InquiryRecordMapper;
import com.fengze.business.vo.HistoryProduct;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.annotation.AutoSetUpdateValue;
import com.fengze.common.config.RuoYiConfig;
import com.fengze.common.utils.DateUtils;
import com.fengze.common.utils.file.FileUploadUtils;
import com.fengze.framework.config.ServerConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.ProductsMapper;
import com.fengze.business.domain.Products;
import com.fengze.business.service.IProductsService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品信息Service业务层处理
 * 
 * @author lg
 * @date 2024-01-31
 */
@Service
public class ProductsServiceImpl implements IProductsService 
{
    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private FzProductsCategoryMapper categoryMapper;

    @Autowired
    private InquiryRecordMapper inquiryRecordMapper;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询产品信息
     * 
     * @param id 产品信息主键
     * @return 产品信息
     */
    @Override
    public Products selectProductsById(Long id)
    {
        return productsMapper.selectProductsById(id);
    }

    /**
     * 查询产品信息列表
     * 
     * @param products 产品信息
     * @return 产品信息
     */
    @Override
    public List<Products> selectProductsList(Products products)
    {
        return productsMapper.selectProductsList(products);
    }

    /**
     * 新增产品信息
     * 
     * @param products 产品信息
     * @return 结果
     */
    @AutoSetInsertValue
    @Override
    public int insertProducts(Products products)
    {
        products.setCreateTime(DateUtils.getNowDate());
        String categoryIds = products.getCategoryPids();
        String categoryNames = null;
        String categoryIdsStr = null;

        if (categoryIds.contains(",")) {
            String[] split = categoryIds.split(",");
            Long categoryId = Long.valueOf(split[split.length -1 ]);
            FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(categoryId);
            // 组合类别名称
            categoryNames = combination("",category);
            // 组合类别id
            categoryIdsStr = combinationId("",category);
        }else{
            FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(Long.valueOf(categoryIds));
            // 组合类别名称
            categoryNames = combination("",category);
            // 组合类别id
            categoryIdsStr = combinationId("",category);
        }
        categoryNames = StringUtils.removeEnd(categoryNames,"-");
        products.setCategoryNames(categoryNames);
        categoryIdsStr = StringUtils.removeEnd(categoryIdsStr,",");
        products.setCategoryPids(categoryIdsStr);


        return productsMapper.insertProducts(products);
    }

    private String combinationId(String idStr, FzProductsCategory category) {
        String categoryIdStr = category.getId().toString();
        categoryIdStr = categoryIdStr + "," + idStr;
        if(category.getPid().longValue() != 0){
            FzProductsCategory parentCategory = categoryMapper.selectFzProductsCategoryById(category.getPid());
            categoryIdStr = combinationId(categoryIdStr,parentCategory);

        }
        return categoryIdStr;
    }

    private String combination(String categoryNames, FzProductsCategory category) {
        String categoryName = category.getName();
        categoryNames = categoryName + "-" + categoryNames;
        if(category.getPid().longValue() != 0){
            FzProductsCategory parentCategory = categoryMapper.selectFzProductsCategoryById(category.getPid());
            categoryNames = combination(categoryNames,parentCategory);

        }
        return categoryNames;
    }

    /**
     * 修改产品信息
     * 
     * @param products 产品信息
     * @return 结果
     */
    @AutoSetUpdateValue
    @Override
    public int updateProducts(Products products)
    {
        String categoryIds = products.getCategoryPids();
        Products dbproducts = productsMapper.selectProductsById(products.getId());
        String categoryNames = null;
        String categoryIdsStr = null;
        if(!categoryIds.equals(dbproducts.getCategoryPids())){
            if (categoryIds.contains(",")) {
                String[] split = categoryIds.split(",");
                Long categoryId = Long.valueOf(split[split.length -1 ]);
                FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(categoryId);
                categoryNames = combination("",category);
                // 组合类别id
                categoryIdsStr = combinationId("",category);
            }else{
                FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(Long.valueOf(categoryIds));
                categoryNames = combination("",category);
                // 组合类别id
                categoryIdsStr = combinationId("",category);
            }
            categoryNames = StringUtils.removeEnd(categoryNames,"-");
            products.setCategoryNames(categoryNames);
            categoryIdsStr = StringUtils.removeEnd(categoryIdsStr,",");
            products.setCategoryPids(categoryIdsStr);
        }

        products.setUpdateTime(DateUtils.getNowDate());
        return productsMapper.updateProducts(products);
    }

    /**
     * 批量删除产品信息
     * 
     * @param ids 需要删除的产品信息主键
     * @return 结果
     */
    @Override
    public int deleteProductsByIds(Long[] ids)
    {
        return productsMapper.deleteProductsByIds(ids);
    }

    /**
     * 删除产品信息信息
     * 
     * @param id 产品信息主键
     * @return 结果
     */
    @Override
    public int deleteProductsById(Long id)
    {
        return productsMapper.deleteProductsById(id);
    }


    @AutoSetInsertValue
    @Override
    public int insertProductsWithImg(Products products, MultipartFile imgFile)  {
        if (imgFile != null && !imgFile.isEmpty()) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, imgFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
//            String url = serverConfig.getUrl() + fileName;
//            System.out.println(url);
            products.setImgUrl(fileName);
        }



        String categoryIds = products.getCategoryPids();
        String categoryNames = null;
        String categoryIdsStr = null;

        if (categoryIds.contains(",")) {
            String[] split = categoryIds.split(",");
            Long categoryId = Long.valueOf(split[split.length -1 ]);
            FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(categoryId);
            // 组合类别名称
            categoryNames = combination("",category);
            // 组合类别id
            categoryIdsStr = combinationId("",category);
        }else{
            FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(Long.valueOf(categoryIds));
            // 组合类别名称
            categoryNames = combination("",category);
            // 组合类别id
            categoryIdsStr = combinationId("",category);
        }
        categoryNames = StringUtils.removeEnd(categoryNames,"-");
        products.setCategoryNames(categoryNames);
        categoryIdsStr = StringUtils.removeEnd(categoryIdsStr,",");
        products.setCategoryPids(categoryIdsStr);


        return productsMapper.insertProducts(products);
    }

    @Override
    public int updateProductsWithFile(Products products, MultipartFile imgFile) {
        if (imgFile != null && !imgFile.isEmpty()) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = null;
            try {
                fileName = FileUploadUtils.upload(filePath, imgFile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
//            String url = serverConfig.getUrl() + fileName;
            products.setImgUrl(fileName);
        }

        String categoryIds = products.getCategoryPids();
        Products dbproducts = productsMapper.selectProductsById(products.getId());
        String categoryNames = null;
        String categoryIdsStr = null;
        if(!categoryIds.equals(dbproducts.getCategoryPids())){
            if (categoryIds.contains(",")) {
                String[] split = categoryIds.split(",");
                Long categoryId = Long.valueOf(split[split.length -1 ]);
                FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(categoryId);
                categoryNames = combination("",category);
                // 组合类别id
                categoryIdsStr = combinationId("",category);
            }else{
                FzProductsCategory category = categoryMapper.selectFzProductsCategoryById(Long.valueOf(categoryIds));
                categoryNames = combination("",category);
                // 组合类别id
                categoryIdsStr = combinationId("",category);
            }
            categoryNames = StringUtils.removeEnd(categoryNames,"-");
            products.setCategoryNames(categoryNames);
            categoryIdsStr = StringUtils.removeEnd(categoryIdsStr,",");
            products.setCategoryPids(categoryIdsStr);
        }

        products.setUpdateTime(DateUtils.getNowDate());
        return productsMapper.updateProducts(products);
    }

    @Override
    public List<HistoryProduct> selectProductsHistoryList(Products products) {
        return inquiryRecordMapper.selectHistoryList(products);
    }
}
